package com.quick.pai;

import com.alibaba.fastjson.JSON;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName PaiOne
 * @Description 切割法
 * π≈3*2^n*y_n
 * 其中，n代表割圆次数，y_n代表圆中内嵌正6*n边形的边长
 * 因为使用了double，会有丢失精度的问题
 * @Author Wangxin
 * @Date 2019-03-03 22:48
 * @Version 1.0
 **/
public class PaiOne {
    public static void main(String[] args) {
        cut(100);

        Map<String,Map<String,String>> aa = new HashMap<>();
        Map<String,String> sss = aa.computeIfAbsent("aaa",k->new HashMap<>());
        sss.put("bb","bb");

        sss =aa.computeIfAbsent("bb",k->new HashMap<>());
        sss.put("aaa","aaa");
        System.out.println(aa);

        List<String> ll = Arrays.asList("uu","222","333");
        Map<String,Integer> a = IntStream.range(0, ll.size())
                .boxed()
                .map(i -> new AbstractMap.SimpleEntry<>(ll.get(i), i))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (p1, p2) -> p1));
        System.out.println(a);

        Map<Integer,String> mm = IntStream.range(0, ll.size()).boxed().collect(Collectors.toMap(Function.identity(), ll::get));
        System.out.println(mm);

        int num[] = IntStream.range(0, ll.size()).toArray();
        System.out.println(JSON.toJSON(num));

        LinkedList<List<Integer>> res = new LinkedList<>();
        res.add(new ArrayList<>());
        for (int n : num) {

            int size = res.size();
            for (; size > 0; size--) {
                List<Integer> r = res.pollFirst();
                for (int i = 0; i <= r.size(); i++) {
                    List<Integer> t = new ArrayList<>(r);
                    t.add(i, n);
                    res.add(t);
                }
            }
        }
        System.out.println(JSON.toJSON(res));

    }
    public static void cut(int n){
        double y = 1.0;
        for (int i = 0; i <=n ; i++) {
            double π = 3 * Math.pow(2,i) * y;
            System.out.println("y="+y);
            System.out.println("第"+i+"次切割,为正"+(6+6*i)+"边形，圆周率π≈"+π);
            y = Math.sqrt(2-Math.sqrt(4-y*y));
        }
    }
}
